package com.ruoyi.invest.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.invest.domain.TbInvest;
import com.ruoyi.invest.domain.TbInvestExample;
import com.ruoyi.invest.domain.TbUser;
import com.ruoyi.invest.mapper.TbInvestMapper;
import com.ruoyi.invest.mapper.TbProductMapper;
import com.ruoyi.invest.mapper.TbUserMapper;
import com.ruoyi.invest.service.InvestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class InvestServiceImpl implements InvestService {
    private static final Logger log = LoggerFactory.getLogger(InvestServiceImpl.class);

    @Autowired
    private TbInvestMapper investMapper;

    @Autowired
    private TbUserMapper userMapper;


    @Override
    public AjaxResult list(TbInvest invest) {

        if(invest.getUserId()==null){
            TbInvestExample example=new TbInvestExample();
            TbInvestExample.Criteria criteria = example.createCriteria();
            if(invest.getKeyWord()!=null){
                criteria.andInvestNameEqualTo(invest.getKeyWord());
            }
            PageHelper.startPage(invest.getPageNum(),invest.getPageSize());
            PageInfo page=new PageInfo(investMapper.selectByExample(example));

            Map map=new HashMap();
            map.put("list",page.getList());
            map.put("total",page.getTotal());
            return new AjaxResult(200,"查询成功",map);
        }

        PageHelper.startPage(invest.getPageNum(),invest.getPageSize());
        PageInfo page=new PageInfo(investMapper.listuserid(invest));

        Map map=new HashMap();
        map.put("list",page.getList());
        map.put("total",page.getTotal());
        return new AjaxResult(200,"查询成功",map);
    }

    @Override
    public AjaxResult insert(TbInvest invest) {

        //根据id获取,并扣减金额
        TbUser user = userMapper.selectByPrimaryKey(invest.getUserId());
        user.setUserAmount(user.getUserAmount()-invest.getInvestPrice());
        //扣减金额
        userMapper.updateByPrimaryKeySelective(user);

        invest.setCreateBy(user.getNickName());
        investMapper.insertSelective(invest);
        return new AjaxResult(200,"成功");
    }

    @Override
    public AjaxResult selectbyproductid(TbInvest invest) {
        TbInvest in = investMapper.selectbyproductid(invest);

        if(in!=null){
            return new AjaxResult(200,"失败");
        }

        return AjaxResult.success();
    }

    @Override
    public AjaxResult update(TbInvest invest) {
        //根据id获取,并扣减金额
        TbUser user = userMapper.selectByPrimaryKey(invest.getUserId());
        user.setUserAmount(user.getUserAmount()-invest.getInvestPrice());
        //扣减金额
        userMapper.updateByPrimaryKeySelective(user);

        TbInvest in = investMapper.selectByPrimaryKey(invest.getInvestId());
        in.setInvestPrice(in.getInvestPrice()+invest.getInvestPrice());

        investMapper.updateByPrimaryKeySelective(in);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult del(TbInvest invest) {

        invest.setDelFlag(1);
        investMapper.updateByPrimaryKeySelective(invest);
        return AjaxResult.success();
    }
}
